package com.one.util;

import java.sql.*;
import java.util.Vector;

public class GetBookDataFromSql {
    public static String url = CheckCodeFromMysql.url;

    public static Vector<Vector<Object>> getBookData () {
        Vector<Vector<Object>>  bookData = new Vector<>();

        //行数据
        String bookId;
        String bookName;
        String bookAuthor;
        String bookPublisher;
        float bookValue;
        int bookAllNum;
        int bookFreeNum;

        try {
            Connection connection = DriverManager.getConnection(url, "root", "123456");
            Statement statement = connection.createStatement();
            statement.execute("select * from books");
            ResultSet resultSet = statement.getResultSet();


            while (resultSet.next()) {
                Vector<Object> bookRow = new Vector<>();
                bookId = resultSet.getString("bookId");
                bookName = resultSet.getString("bookName");
                bookAuthor = resultSet.getString("bookAuthor");
                bookPublisher = resultSet.getString("bookPublisher");
                bookValue = resultSet.getFloat("bookValue");
                bookAllNum = resultSet.getInt("bookAllNum");
                bookFreeNum = resultSet.getInt("bookFreeNum");

                bookRow.add(bookId);
                bookRow.add(bookName);
                bookRow.add(bookAuthor);
                bookRow.add(bookPublisher);
                bookRow.add(bookValue);
                bookRow.add(bookAllNum);
                bookRow.add(bookFreeNum);

                bookData.add(bookRow);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
        return bookData;
    }

//    public static void main(String[] args) {
//        Vector<Vector<Object>> data = getBookData();
//        for (Vector<Object> Row: data) {
//            for (Object object : Row) {
//                System.out.print(object + ", ");
//            }
//            System.out.println();
//        }
//    }
}
